/**
 * @author lteng
 * @time 2022-06-19 09:45:11 
 */

package liteng.study.cn.stuscore.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import liteng.study.cn.stuscore.entity.User;
import liteng.study.cn.stuscore.service.UserService;
import liteng.study.cn.stuscore.service.impl.UserServiceImpl;

/**
 * 登录功能
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	private UserService userService=new UserServiceImpl(); 
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		System.out.println(new Date().toString());
		String userName = request.getParameter("usr");
		String passWord = request.getParameter("pwd");
		
		User user = userService.getUserByName(userName);
		System.out.println(user);
		if (user==null) {
			
			request.setAttribute("error", "用户不存在");
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}else {
			if (user.getPassWord().equals(passWord)) {
				request.getSession().setAttribute("user", user);
				response.sendRedirect("/stuscore/pages/index.jsp");
			}else {
				request.setAttribute("error", "密码错误");
				
				request.getRequestDispatcher("/login.jsp").forward(request, response);
			}
		}
		
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
